package com.me.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/checkKaptcha")
public class CheckKaptchaServlet extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userVerifyCode = request.getParameter("verify");
        // 生成验证码后，kaptcha会把验证码写入session中以KAPTCHA_SESSION_KEY为名字保存
        String verifyCode = (String) request.getSession().getAttribute("KAPTCHA_SESSION_KEY");
        String msg = null;
        if (userVerifyCode.equals(verifyCode)) {
            msg = "1";
        } else {
            msg = "0";
        }
        response.setContentType("text/html;charset=utf8");
        response.getWriter().println(msg);
    }
}
